home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRSTRL.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  100 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7.  
  8. ;
  9. ;
  10. ; strstrl- Returns the position of a substring in another string.
  11. ;
  12. ; inputs:
  13. ;
  14. ;    es:di- address of string to search through.
  15. ;    return address- address of substring to search for.
  16. ;
  17. ;
  18. ; returns: 
  19. ;
  20. ;    cx- position of character in string (if present).
  21. ;    carry=0 if character found.
  22. ;    carry=1 if character is not present in string.
  23. ;
  24.         public    sl_strstrl
  25. ;
  26. sl_strstrl    proc    far
  27.         push    bp
  28.         mov    bp, sp
  29.         push    ds
  30.         push    es
  31.         pushf
  32.         push    si
  33.         push    di
  34.         push    ax
  35.         push    bx
  36.         push    dx
  37.         cld
  38.         mov    ax, es
  39.         mov    ds, ax
  40.         mov    si, di
  41.         les    di, 2[bp]
  42. ;
  43.         mov    bx, di        ;Save ptr to substring.
  44. ;
  45. ; Compute the length of the substring:
  46. ;
  47.         mov    cx, 0ffffh
  48.         mov    al, 0
  49.     repne    scasb
  50.         neg    cx
  51.         dec    cx
  52.         dec    cx
  53.         mov    dx, cx        ;Save length of smaller string.
  54.         mov    2[bp], di    ;Save new return address.
  55. ;
  56.         mov    ax, si        ;Save ptr to string.
  57. StrLp:        mov    cx, dx
  58.     repe    cmpsb            ;Compare the strings
  59.         jz    StrsAreEql    ;Jump if substring exists.
  60.         inc    ax        ;Bump pointer into string.
  61.         mov    si, ax        ;Restore pointers.
  62.         mov    di, bx
  63.         cmp    byte ptr [si], 0 ;Done yet?
  64.         jne    StrLp
  65. ;
  66. ; Bad news down here, the substring isn't present in the source string.
  67. ;
  68.         xor    cx, cx
  69.         pop    dx
  70.         pop    bx
  71.         pop    ax
  72.         pop    di
  73.         pop    si
  74.         popf
  75.         pop    es
  76.         pop    ds
  77.         pop    bp
  78.         stc
  79.         ret
  80. ;
  81. StrsAreEql:
  82.         mov    cx, ax            ;Save ptr to string
  83.         pop    dx
  84.         pop    bx
  85.         pop    ax
  86.         pop    di
  87.         sub    cx, di            ;Compute index to substring.
  88.         pop    si
  89.         popf
  90.         clc
  91.         pop    es
  92.         pop    ds
  93.         pop    bp
  94.         ret
  95. sl_strstrl    endp
  96. ;
  97. ;
  98. stdlib        ends
  99.         end
  100.